
var constant = require('../config/constant.config');

merchantInprocessController.$inject = ['$location','merchantService','$scope','$rootScope']
function merchantInprocessController($location,merchantService,$scope,$rootScope) {
    $rootScope.pendingCountStatus = 0;

    $scope.dateParam ='?a='+(new Date().getTime());
    $scope.imgUrl = constant.IMG_CONTEXT;
    //init data
    merchantService.findMerchantByStatus(0)
        .then(function(res){
            var rs = res.data;
            if(!rs.result){
                alert(rs.errMsg);
            }else {
                $scope.merchants = rs.data;
            }
        });

    //view the merhcant details
    $scope.toProcess = function(mer){
        $scope.detailMerchant = mer;
    }

    //process merchant
    $scope.process = function(mer){
        mer.status = $("#usertype").val();
        console.log(JSON.stringify(mer));
        if(mer.rejectContent){
            merchantService.updateMerchant(mer)
                .then(function(res){
                    processResult(res,mer);
                });
        }else {
            merchantService.updateStatus(mer.status,mer._id)
                .then(function(res){
                    processResult(res,mer);
                });
        }
    }

    //deal result
    function processResult(res,mer){
        var rs = res.data;
        if(!rs.result){
            alert(rs.errMsg);
        }else {
            alert('process success');
            $scope.merchants = $scope.merchants.filter(function(item){
                return item._id != mer._id;
            });
            $("#processModal").modal('hide');
        }
    }


    //
    // $scope.isReject = false;
    // $("#usertype").bind("change",function(e){
    //     alert(111);
    //     $scope.isReject = !$scope.isReject;
    //     console.log($scope.isReject);
    // });
    //驳回意见显示和隐藏
    $("#rejectReason").hide();
    var flag = false;
    $("#usertype").bind("change",function(e){
        if(flag){
            $("#rejectReason").hide();
        }else {
            $("#rejectReason").show();
        }
        flag=!flag;
    });
}

module.exports = merchantInprocessController;